﻿#ifndef COMSERVERMANAGE_H
#define COMSERVERMANAGE_H

#include <QObject>
#include <QThread>
#include "TcpServer.h"
#include "WidgetComServer.h"
#include "ComServerStatusWidget.h"


class ComServerManage : public QObject
{
    Q_OBJECT
public:
    explicit ComServerManage(QObject *parent = nullptr);
    QWidget* getComServerStatusWidget(){return wgtComServerStatus;}
    QWidget* getComServerManageWidget(){return mComServerManageWidget;}
    QMap<QString, TcpServer*> getComServerAll(){return mMapComServer;}
    //
    void setConfigWriteOneFunc(void (*func)(const TcpComConfig& config));
    void setConfigReadAllFunc(QMap<QString, TcpComConfig> (*func)());
    void setConfigReadOneFunc(TcpComConfig (*func)(QString camName));
    void startWorker();
    void checkComServerStatus();
    void initComServerAll();
    void initWgtComServerStatus();
    void initComServerManageWidget();

public slots:
    void onAddOne(const TcpComConfig& config);
    void onDeleteOne(const QString& name);
    void onConfigChanged(const TcpComConfig& config);
signals:
    void sigComServerStatus(bool isOnLine);

private:
    void initComServerOne(TcpComConfig config);

private:
    //函数指针，用于配置读写配置的函数
    void (*configWriteOne)(const TcpComConfig& config);
    QMap<QString, TcpComConfig> (*configReadAll)();
    TcpComConfig (*configReadOne)(QString comName);

private:
    QMap<QString, TcpServer*> mMapComServer;
    QMap<QString, QThread*> mMapComThread;
    QMap<QString, bool> mMapComStart;
    ComServerStatusWidget* wgtComServerStatus;
    WidgetComServer* mComServerManageWidget;
};

#endif // COMSERVERMANAGE_H
